package com.sjq.project.service.impl;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sjq.common.entity.LoginAccount;
import com.sjq.common.util.FTPClientHelper;
import com.sjq.common.util.PageResults;
import com.sjq.common.util.PrimaryKeyUtil;
import com.sjq.project.dao.ProjectFileDao;
import com.sjq.project.dao.ProjectFlowDao;
import com.sjq.project.entity.ProjectFile;
import com.sjq.project.entity.ProjectFlow;
import com.sjq.project.service.ProjectFileService;

@Service
public class ProjectFileServiceImpl implements ProjectFileService{
	@Resource
	private ProjectFileDao dao;
	@Resource
	private ProjectFlowDao flowDao;
	
	/**新增  **/
	@Override
	public JSONObject addFile(ProjectFile file,LoginAccount loginAccount) {
		/**标准注册登记人信息***/
		/*Result<File> result=new Result<File>();
		file.setDjdw_gajgjgdm(loginAccount.getUserDwbm());
		file.setDjdw_dwmc(loginAccount.getUserDwmc());
		file.setDjr_gmsfhm(loginAccount.getUserId());
		file.setDjr_xm(loginAccount.getUserName());
		*/
		int count=dao.addFile(file);
		JSONObject json =new JSONObject();
		if(count>0){
			json.put("code","200");
			json.put("msg","ok");
			//蓝信注册通知
			ProjectFlow flow=new ProjectFlow();
		
			flowDao.addFlow(flow);
		}else{
			json.put("code","300");
			json.put("msg","error");
		}
		return json;
	}
	/**新增含有附件的文档上传至FTP***/
	@Override
	public JSONObject addFileFilesByFTP(ProjectFile file, CommonsMultipartFile[]  fj,LoginAccount loginAccount) {
		//
		String remotePath="\\"+loginAccount.getAccountId()+"\\";//远程登录默认同步至当前身份证号码文件夹下面
		for(int i=0;i<fj.length;i++){
			String originalFileName=fj[i].getOriginalFilename();//获取原文件名称
			String suffix=	originalFileName.substring(originalFileName.lastIndexOf(".")+1);//获取源文件类型
			String newFileName=PrimaryKeyUtil.getUUID()+""+suffix;	//将文件重新命名
			boolean flage=false;
			try {
				FTPClientHelper ftp=new FTPClientHelper(remotePath);
				flage = ftp.upload(fj[i].getInputStream(), newFileName);
			} catch (IOException e) {
				e.printStackTrace();
			}
			//是否上传成功
			if(flage){
				/***上传成功，将附件信息插入数据库***/
				String url=remotePath+newFileName;//
				file.setXxzjbh(PrimaryKeyUtil.getUUID());//主键id
				file.setWed_mc(originalFileName);//文档原始名称
				file.setWjlx(suffix);//文档后缀名称
				file.setWjcclj(url);//ftp文档url地址
				int count=dao.addFile(file);	
			}else{
			
			}
		}
		int count=0;	
		JSONObject json =new JSONObject();
		if(count>0){
			json.put("code","200");
			json.put("msg","ok");
		}else{
			json.put("code","300");
			json.put("msg","error");
		}
		return json;
	}
	
	
	
	/**删除 **/
	@Override
	public JSONObject deleteFile(String fileId,LoginAccount loginAccount) {
		int count=dao.deleteFile(fileId);
		JSONObject json =new JSONObject();
		if(count>0){
			json.put("code","200");
			json.put("msg","ok");
		}else{
			json.put("code","300");
			json.put("msg","error");
		}
		return json;
	}
	
	/**更新 **/
	@Override
	public JSONObject updateFile(ProjectFile file,LoginAccount loginAccount) {
		//跟新人信息
		/*
		file.setXgr_gajgjgdm(loginAccount.getUserDwbm());
		file.setXgr_dwmc(loginAccount.getUserDwmc());
		file.setXgr_gmsfhm(loginAccount.getUserId());
		file.setXgr_xm(loginAccount.getUserName());
		*/
		JSONObject result=new JSONObject();
		int count=dao.updateFile(file);
		if(count>0){
			result.put("msg", "success");
			result.put("code","200");
		}else{
			result.put("msg", "error");
			result.put("code","300");
		}
		return result;
	}
	
	/**查询**/
	@Override
	public PageResults<?> findFileByPage(int pageNo, int pageSize, ProjectFile file) {
		PageResults<?> pageResults=dao.selectFileByPage(pageNo, pageSize, file);
		return pageResults;
	}
	
	/**根据查询条件查询所有的数据**/
	@Override
	public JSONObject findFileByListAll(ProjectFile file,LoginAccount la){
		List<Map<String, String>> list=dao.findFileByListAll(file);
		/**
		* 权限校验
		**/
		JSONObject result=new JSONObject();
		result.put("msg", "success");
		result.put("code","200");
		result.put("data",list);
		return result;
	}
	/*****/
	
}